﻿using System.Collections.Generic;
using System.Linq;

namespace FileMatcher
{
    public class SelectAndActionOperation
    {
        public readonly IList<FileInfoEx> Selected = new List<FileInfoEx>();

        public readonly IdenticalFileList Source;

        public readonly ActionTypes ActionType;

        public enum ActionTypes
        {
            Delete,
            Undelete
        }

        public SelectAndActionOperation(IdenticalFileList source, ActionTypes actionType)
        {
            Source = source;
            ActionType = actionType;
        }

        public void Select(FileInfoEx sel)
        {
            if (ActionType == ActionTypes.Delete)
            {
                if (!sel.IsSelectedToDelete)
                {
                    Selected.Add(sel);
                }
            }
            else
            {
                if (sel.IsSelectedToDelete)
                {
                    Selected.Add(sel);
                }
            }
        }

        private void DeleteSelected()
        {
            foreach (var sel in Selected)
            {
                sel.IsSelectedToDelete = true;
            }
        }

        private void UndeleteDeselected()
        {
            foreach (var sel in Selected)
            {
                sel.IsSelectedToDelete = false;
            }
        }

        public void Redo()
        {
            if (ActionType == ActionTypes.Delete)
            {
                DeleteSelected();
            }
            else
            {   // ActionType == ActionTypes.Undelete
                UndeleteDeselected();
            }
        }

        public void Undo()
        {
            if (ActionType == ActionTypes.Delete)
            {
                UndeleteDeselected();
            }
            else
            {   // ActionType == ActionTypes.Undelete
                DeleteSelected();
            }
        }
    }
}
